﻿Public Class Frmusuario

    Dim classController As New Class_RC_Controller.rc_Mantenimiento

    Private _codUsuario As Integer = 0

    Public Property codUsuario() As Integer
        Get
            Return _codUsuario
        End Get
        Set(value As Integer)
            _codUsuario = value
        End Set
    End Property

    Private _Pass As String = ""

    Public Property Pass() As String
        Get
            Return _Pass
        End Get
        Set(value As String)
            _Pass = value
        End Set
    End Property

    Private Sub Frmusuario_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        CargarCombo()
        CargarUsuario()

    End Sub

    Private Sub CargarCombo()
        Dim dsArea As DataTable
        dsArea = classController.get_Area_All(oiml.dbConn, 0, "")
        If Not dsArea Is Nothing Then
            With Me.cboArea
                .DataSource = dsArea
                .DisplayMember = "descArea"
                .ValueMember = "codArea"
            End With

        Else
            cboArea.DataSource = Nothing

        End If
    End Sub

    Private Sub CargarUsuario()
        Try
            If codUsuario > 0 Then

                Dim ds As New DataTable
                ds = classController.get_Usuario_All(oiml.dbConn, codUsuario, "", "", "", 0)

                If Not ds Is Nothing Then
                    txtcodusuario.Text = ds.Rows(0)("codUsuario").ToString.PadLeft(4, "0")
                    txtusuario.Text = ds.Rows(0)("usuario").ToString
                    Pass = ds.Rows(0)("pass").ToString
                    txtpass1.Text = ds.Rows(0)("pass").ToString
                    txtcomfirmar.Text = ds.Rows(0)("pass").ToString

                    txtnombre.Text = ds.Rows(0)("nombre").ToString
                    txtapellido.Text = ds.Rows(0)("apellido").ToString
                    txtci.Text = ds.Rows(0)("ci").ToString
                    cboArea.SelectedValue = ds.Rows(0)("area")
                    dtpfecha.Value = ds.Rows(0)("fechaAlta")
                    txtfechabaja.Text = ds.Rows(0)("fechaBaja_").ToString

                    If ds.Rows(0)("Active_") = "SI" Then
                        cbxActivo.Checked = True
                    Else
                        cbxActivo.Checked = False
                    End If

                    cbxActivo.Enabled = True
                    btneliminar.Enabled = True

                End If

            Else

                btneliminar.Enabled = False
                cbxActivo.Checked = True
                cbxActivo.Enabled = False
                txtusuario.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btncancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btneliminar.Click

        If codUsuario > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Usuarios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Usuario(oiml.dbConn, codUsuario) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Usuarios", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtusuario.Text = "" Then
            ErrorProvider1.SetError(txtusuario, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtusuario, "")
        End If        '

        If codUsuario = 0 Then

            If txtpass1.Text = "" Then
                ErrorProvider1.SetError(txtpass1, "Campo obligatorio")
                controlCampos = True
            Else
                ErrorProvider1.SetError(txtpass1, "")
            End If        '

            If txtcomfirmar.Text = "" Then
                ErrorProvider1.SetError(txtcomfirmar, "Campo obligatorio")
                controlCampos = True
            Else
                ErrorProvider1.SetError(txtcomfirmar, "")
            End If        '

            If txtcomfirmar.Text <> txtpass1.Text Then
                ErrorProvider1.SetError(txtcomfirmar, "Las contraseñas deben ser iguales")
                ErrorProvider1.SetError(txtpass1, "Las contraseñas deben ser iguales")
                controlCampos = True
            Else
                ErrorProvider1.SetError(txtcomfirmar, "")
                ErrorProvider1.SetError(txtpass1, "")
            End If        '           

        End If

        If txtnombre.Text = "" Then
            ErrorProvider1.SetError(txtnombre, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtnombre, "")
        End If        '

        If txtapellido.Text = "" Then
            ErrorProvider1.SetError(txtapellido, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtapellido, "")
        End If        '

        If txtci.Text = "" Then
            ErrorProvider1.SetError(txtci, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtci, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(sender As System.Object, e As System.EventArgs) Handles btnguardar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If codUsuario = 0 Then

            codUsuario = classController.Insert_Usuario(oiml.dbConn, txtusuario.Text, txtcomfirmar.Text, txtnombre.Text, txtapellido.Text, txtci.Text, cboArea.SelectedValue)

            If codUsuario > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Usuarios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Usuarios", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            Dim passParam As String = ""

            If txtpass1.Text <> "" And txtcomfirmar.Text <> "" Then
                If Pass <> txtcomfirmar.Text Then
                    passParam = txtcomfirmar.Text
                End If
            End If
            
            If classController.Update_Usuario(oiml.dbConn, txtusuario.Text, passParam, txtnombre.Text, txtapellido.Text, txtci.Text, cboArea.SelectedValue, codUsuario, cbxActivo.Checked, oiml.idUsuario) Then
                MessageBox.Show("Registro grabado con exito!", "Usuarios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Usuarios", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

End Class